package io;

import java.io.*;

/**
 * io流练习
 */
public class IoExercise02 {

    public static void main(String[] args) {
        /*
            使用BufferedReader读取一个文本文件(编码gbk)，为每行加上行号，
            再连同内容一并输出到屏幕上

            提示：使用转换流防止乱码
         */

        String filePath = "D:\\IdeaCode\\javaStudy\\exercise.txt";

        InputStreamReader isr = null;
        BufferedReader br = null;
        String line = null;
        int lineNum = 1;

        try {
            isr = new InputStreamReader(new FileInputStream(filePath), "GBK");
            br = new BufferedReader(isr);
            while ((line = br.readLine()) != null){
                System.out.println((lineNum++) + ":" + line);
            }
        } catch (Exception e) {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}
